package com.whpost.service.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whpost.service.dao.t.MyDao;
import com.whpost.service.dao.t.TCzyDao;
import com.whpost.service.dao.t.THyxxDao;
import com.whpost.service.entity.t.TreeNode;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.tools.Aes;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.tools.XSSFilterJSON;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

public abstract class WxBaseServices<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IBaseService {

@Resource
private THyxxDao tHyxxDao;
@Resource
private MyDao myDao;

/**
 * 加密返回 200和500只表示是否返回成功，具体业务逻辑，参照data里面的code;
 * @param resultInfo
 * @param loginId
 * @return
 */
public ResultInfo backRtn(ResultInfo resultInfo,String loginId) {
    JSONObject jsonObject1;
    String data="";
    ToolBean.info("backRtn获取的resultInfo:["+resultInfo.toString()+"]");
    try{
        jsonObject1 =  (JSONObject) JSONObject.toJSON(resultInfo.getData());
        data = JSONObject.toJSONString(jsonObject1);
    }catch (Exception e){
        ToolBean.info("backRtn获取的data发生错误:["+e.toString()+"]");
        data = (String)resultInfo.getData();
    }
    try{
        ToolBean.info("backRtn获取的data:["+data+"]");
        String aeskey = gettHyxxDao().getAesKey(loginId);
        String rtnStr = Aes.encrypt(data,aeskey);
        ToolBean.info("backRtn准备返回的rtnStr:["+rtnStr+"]");
        return new ResultInfo(resultInfo.getCode(), resultInfo.getMsg(),rtnStr);
    }catch (Exception e){
        ToolBean.info("BaseServices.backRtn返回失败！");
        return new ResultInfo(500, "返回失败！",null);
    }
}

    /**
     * 获取前端map进行解密
     * @param map
     * @param loginId
     * @return
     */
    public JSONObject getData(Map<String, Object> map,String loginId) {
    JSONObject jsonObjectMap = (JSONObject) JSON.toJSON(map);
    String data = "";
    if (jsonObjectMap.containsKey("data")) data = jsonObjectMap.getString("data");
    ToolBean.info("getData接收的data："+data);
    String aeskey = gettHyxxDao().getAesKey(loginId);
    String dec_data = Aes.decrypt(data,aeskey);
    ToolBean.info("getData接收的data解密后："+dec_data);
    dec_data = XSSFilterJSON.xssFilterJson(dec_data);
    ToolBean.info("getData接收的data解密后过滤后："+dec_data);
    JSONObject jsonObject = JSONObject.parseObject(dec_data);
    return jsonObject;
}

    /**
     * 获取前端字符串进行解密
     * @param data
     * @param loginId
     * @return
     */
    public JSONObject getDataStr(String data,String loginId) {
    ToolBean.info("getDataStr接收的data："+data);
    String aeskey = gettHyxxDao().getAesKey(loginId);
    String dec_data = Aes.decrypt(data,aeskey);
    ToolBean.info("getDataStr接收的data解密后："+dec_data);
    dec_data = XSSFilterJSON.xssFilterJson(dec_data);
    ToolBean.info("getDataStr接收的data解密后过滤后："+dec_data);
    JSONObject jsonObject = JSONObject.parseObject(dec_data);
    return jsonObject;
}

    public MyDao getMyDao() {
        return myDao;
    }

    public THyxxDao gettHyxxDao() {
        return tHyxxDao;
    }

    /**
     * 设置示例中的特殊节点状态
     */
    public List<TreeNode> setExampleSpecialNodes(List<TreeNode> rootNodes) {
        for (TreeNode root : rootNodes) {
            if (root.getValue() == -1) { // 销售员根节点
                root.setDisabled(true);
                for (TreeNode child : root.getChildren()) {
                    if (child.getValue() == 1 || child.getValue() == 2) {
                        child.setSelected(true);
                    }
                    if (child.getValue() == 3) {
                        child.setDisabled(true);
                    }
                }
            } else if (root.getValue() == -2) { // 奖品根节点
                for (TreeNode child : root.getChildren()) {
                    if (child.getValue() == 4) {
                        child.setSelected(true);
                        child.setDisabled(true);
                    } else if (child.getValue() == -3) { // 奖品3子树
                        for (TreeNode grandChild : child.getChildren()) {
                            if (grandChild.getValue() == 14) {
                                grandChild.setSelected(true);
                            }
                        }
                    }
                }
            }
        }
        return rootNodes;
    }
}
